package com.autologistics.service;

import java.util.List;

import com.autologistics.dao.MediaDao;
import com.autologistics.model.Goods;
import com.autologistics.model.Media;
import com.autologistics.model.Playlist;
import com.autologistics.util.PageBean;

public class MediaServiceImpl implements MediaService {
	private MediaDao mediaDao;
	private PageBean pageBean;
	

	public PageBean getPageBean() {
		return pageBean;
	}

	public void setPageBean(PageBean pageBean) {
		this.pageBean = pageBean;
	}

	public MediaDao getMediaDao() {
		return mediaDao;
	}

	public void setMediaDao(MediaDao mediaDao) {
		this.mediaDao = mediaDao;
	}

	@Override
	public int addMedia(Media media) {
		// TODO Auto-generated method stub
		mediaDao.add(media);
		return 0;
	}

/*	@Override
	public int deleteMedia(Media media) {
		// TODO Auto-generated method stub
		mediaDao.delete(media);
		return 0;
	}*/

	@Override
	public Media getMedia(Media media) {
		// TODO Auto-generated method stub
		Media get = mediaDao.get(media); 
		return get;
	}

	@Override
	public List<Media> listMedia() {
		// TODO Auto-generated method stub
		List<Media> getList = mediaDao.listAll();
		return getList;
	}
	
	public PageBean queryForOnePage(final int pageSize, int page){
//		System.out.println("userId    "+userId);
		int allRow = mediaDao.allRow();  //总记录数   
		System.out.println("allRow    "+allRow);
		int totalPage = PageBean.countTotalPage(pageSize, allRow); //总页数  
		System.out.println("totalPage    "+totalPage);
		
		final int offset = PageBean.countOffset(pageSize, page); //当前页开始记录  
		System.out.println("offset    "+offset);
		final int length = pageSize; // 每页记录数    
		final int currentPage = PageBean.countCurrentPage(page); // 当前页    
		List<Media> onePageMedialist = mediaDao.queryForOnePage(offset, length); //    
		//把分页信息保存到Bean当中    
		PageBean pageBean  = new PageBean();    
		pageBean.setPageSize(pageSize);    
		pageBean.setCurrentPage(currentPage);    
		pageBean.setAllRow(allRow);    
		pageBean.setTotalPage(totalPage);    
		pageBean.setMediaList(onePageMedialist);    
	//	pageBean.init();    
		return pageBean; 
	}
	
	/**
	 * to get media object through id.
	 */
	public Media getMediaById(int id){
		if(mediaDao.queryMediaById(id)==null){
			return null;
		}else{
			return mediaDao.queryMediaById(id);
		}		
	}
	
	public void updateMedia(Media media){
		this.mediaDao.updateMedia(media);
	}
	
	public void deleteMedia(int mediaId){
		mediaDao.deleteMedia(mediaId);
	}
	
	public List<Media> searchMedia(String filename){
		return this.mediaDao.searchMedia(filename);
	}

	public void addPlayList(Playlist playlist){
		mediaDao.addPlayList(playlist);
	}
}
